package com.lc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.mapper.UmsPcdDicMapper;
import com.lc.mapper.UmsReceiverAddressMapper;
import com.lc.pojo.UmsPcdDic;
import com.lc.pojo.UmsReceiverAddress;
import com.lc.pojo.UmsUser;
import com.lc.pojo.dto.UmsReceiverAddressDto;
import com.lc.pojo.param.SaveOrUpdateParam;
import com.lc.service.UmsReceiverAddressService;
import com.lc.utils.ThreadVariablesUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UmsReceiverAddressServiceImpl extends ServiceImpl<UmsReceiverAddressMapper, UmsReceiverAddress> implements UmsReceiverAddressService {

    @Resource
    private UmsReceiverAddressMapper uraMapper;

    @Resource
    private UmsPcdDicMapper umsPcdDicMapper;

    // 添加、修改收货地址
    @Override
    public Boolean saveOrUpdateURA(SaveOrUpdateParam saveOrUpdateParam) {
        UmsReceiverAddress umsReceiverAddress = new UmsReceiverAddress();
        BeanUtils.copyProperties(saveOrUpdateParam, umsReceiverAddress);

        UmsUser umsUser = ThreadVariablesUtil.getThreadData();
        umsReceiverAddress.setUserId(umsUser.getId());

        if (saveOrUpdateParam.getId() != null) {
            // 如果ID不为空，执行修改操作

            // 取消其他地址的默认状态
            LambdaUpdateWrapper<UmsReceiverAddress> cancelDefaultWrapper = new LambdaUpdateWrapper<>();
            cancelDefaultWrapper.eq(UmsReceiverAddress::getUserId, umsUser.getId())
                    .ne(UmsReceiverAddress::getId, saveOrUpdateParam.getId())
                    .set(UmsReceiverAddress::getDefaultStatus, 0);
            uraMapper.update(null, cancelDefaultWrapper);

            LambdaUpdateWrapper<UmsReceiverAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UmsReceiverAddress::getId, saveOrUpdateParam.getId())
                    .set(UmsReceiverAddress::getDefaultStatus, 1);
            int updateURA = uraMapper.update(umsReceiverAddress,updateWrapper);
            return updateURA > 0;
        } else {
            // 如果ID为空，执行添加操作
            int insertURA = uraMapper.insert(umsReceiverAddress);
            return insertURA > 0;
        }
    }

    //删除收货地址
    @Override
    public Boolean removeURA(Integer addressId) {
        UmsUser umsUser = ThreadVariablesUtil.getThreadData();

        LambdaUpdateWrapper<UmsReceiverAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UmsReceiverAddress::getUserId, umsUser.getId())
                .eq(UmsReceiverAddress::getId, addressId)
                .set(UmsReceiverAddress::getIsDelete, 1);
        int deleteURA = uraMapper.update(null,wrapper);

        return deleteURA > 0;
    }

    //查看收货地址
    @Override
    public List<UmsReceiverAddressDto> getList(Integer userId) {
        LambdaQueryWrapper<UmsReceiverAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsReceiverAddress::getUserId, userId)
                .eq(UmsReceiverAddress::getIsDelete, 0);
        List<UmsReceiverAddress> listURA = uraMapper.selectList(wrapper);

        ArrayList<UmsReceiverAddressDto> listURADto = new ArrayList<>();
        listURA.forEach(ura->{
            UmsReceiverAddressDto dto = new UmsReceiverAddressDto();
            BeanUtils.copyProperties(ura, dto);

            //区
            LambdaQueryWrapper<UmsPcdDic> wrapperCounty = new LambdaQueryWrapper<>();
            wrapperCounty.eq(UmsPcdDic::getId, ura.getCountyId());
            UmsPcdDic county = umsPcdDicMapper.selectOne(wrapperCounty);
            //市
            LambdaQueryWrapper<UmsPcdDic> wrapperCity = new LambdaQueryWrapper<>();
            wrapperCity.eq(UmsPcdDic::getId, county.getParentId());
            UmsPcdDic city = umsPcdDicMapper.selectOne(wrapperCity);
            //省
            LambdaQueryWrapper<UmsPcdDic> wrapperProvince = new LambdaQueryWrapper<>();
            wrapperProvince.eq(UmsPcdDic::getId, city.getParentId());
            List<UmsPcdDic> province = umsPcdDicMapper.selectList(wrapperProvince);

            dto.setCounty(county.getName());
            dto.setCity(city.getName());
            dto.setCityId(city.getId());
            province.forEach(pro->{
                dto.setProvince(pro.getName());
                dto.setProvinceId(pro.getId());
            });

            listURADto.add(dto);
        });

        return listURADto;
    }

    //设置默认收货地址
    @Override
    public Boolean updateDefault(Integer addressId) {
        UmsUser umsUser = ThreadVariablesUtil.getThreadData();

        // 取消其他地址的默认状态
        LambdaUpdateWrapper<UmsReceiverAddress> cancelDefaultWrapper = new LambdaUpdateWrapper<>();
        cancelDefaultWrapper.eq(UmsReceiverAddress::getUserId, umsUser.getId())
                .ne(UmsReceiverAddress::getId, addressId)
                .set(UmsReceiverAddress::getDefaultStatus, 0);
        uraMapper.update(null, cancelDefaultWrapper);

        // 将指定地址设置为默认地址
        LambdaUpdateWrapper<UmsReceiverAddress> setDefaultWrapper = new LambdaUpdateWrapper<>();
        setDefaultWrapper.eq(UmsReceiverAddress::getUserId, umsUser.getId())
                .eq(UmsReceiverAddress::getId, addressId)
                .set(UmsReceiverAddress::getDefaultStatus, 1);
        int updateCount = uraMapper.update(null, setDefaultWrapper);

        return updateCount > 0;
    }



}
